package cn.qingyun.gis.modules.caronline.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.caronline.entity.CarProofread;
import cn.qingyun.gis.modules.caronline.entity.dto.CarExportTotalDto;
import cn.qingyun.gis.modules.caronline.mapper.CarProofreadMapper;
import cn.qingyun.gis.modules.caronline.service.ICarProofreadService;
import cn.qingyun.gis.modules.express.request.ExpressProofreadRequest;
import cn.qingyun.gis.modules.express.request.ExpressQueryRequest;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.entity.SysUser;
import cn.qingyun.gis.modules.init.system.mapper.SysUserMapper;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import cn.qingyun.gis.modules.init.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @Description: 网约车勘误表后台管理
 * @Version: V1.0
 */
@Api(tags = "网约车勘误")
@RestController
@RequestMapping("/app/car/car-proofread")
@Slf4j
public class CarProofreadController extends BasicController<CarProofread, ICarProofreadService> {
    @Resource
    private ICarProofreadService iCarProofreadService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private CarProofreadMapper carProofreadMapper;


    @GetMapping("/query")
    @ApiOperation("网约车未勘误-需传入网格编码、工号（非必填）、区域权限、关键字（非必填）")
    public Result<?> queryList(@Validated ExpressQueryRequest request) {
        return Result.okData(iCarProofreadService.queryList(request));
    }

    @PostMapping("/proofread")
    @ApiOperation("网约车地址勘误")
    public Result<?> proofread(@Validated @RequestBody ExpressProofreadRequest request) {
        return Result.okData(iCarProofreadService.carProofread(request));
    }

    @GetMapping("/queryTotal")
    @ApiOperation("网约车地址已勘误列表查询-需传入网格编码、工号（非必填）、区域权限、关键字（非必填）")
    public Result<?> queryTotalList(@Validated ExpressQueryRequest request) {
        return Result.okData(iCarProofreadService.queryTotalList(request));
    }

    /**
     * 分页列表查询
     *
     * @param carProofread
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "网约车勘误表-分页列表查询")
    @ApiOperation(value = "网约车勘误表-分页列表查询", notes = "网约车勘误表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CarProofread carProofread,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CarProofread> queryWrapper = QueryGenerator.initQueryWrapper(carProofread, req.getParameterMap());
        Page<CarProofread> page = new Page<>(pageNo, pageSize);
        IPage<CarProofread> pageList = iCarProofreadService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    @AutoLog(value = "网约车明细表-分页列表查询")
    @ApiOperation(value = "网约车明细表-分页列表查询", notes = "网约车明细表-分页列表查询")
    @GetMapping(value = "/detailList")
    public Result<?> queryPageDetailList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest request) {

        // 获取当前用户信息
        String currentUserName = JwtUtil.getUserNameByToken(request);
        // 根据用户信息查询区域编码
        SysUser user = sysUserMapper.selectOne(new QueryWrapper<SysUser>().lambda().
                select(SysUser::getRegionCode).
                eq(SysUser::getUsername, currentUserName));
        String regionCode;
        if ("0000SD".equals(user.getRegionCode())) { // 当前登录工号为省级权限
            regionCode = null;
        } else {
            regionCode = user.getRegionCode();
        }
        Page<CarExportTotalDto> page = new Page<>(pageNo, pageSize);
        IPage<CarExportTotalDto> pageList = carProofreadMapper.selectCarTotal(page, null, regionCode);
        return Result.okData(pageList);
    }


    /**
     * 添加
     *
     * @param carProofread
     * @return
     */
    @AutoLog(value = "网约车勘误表-添加")
    @ApiOperation(value = "网约车勘误表-添加", notes = "网约车勘误表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CarProofread carProofread) {
        iCarProofreadService.save(carProofread);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param carProofread
     * @return
     */
    @AutoLog(value = "网约车勘误表-编辑")
    @ApiOperation(value = "网约车勘误表-编辑", notes = "网约车勘误表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody CarProofread carProofread) {
        iCarProofreadService.updateById(carProofread);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "网约车勘误表-通过id删除")
    @ApiOperation(value = "网约车勘误表-通过id删除", notes = "网约车勘误表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        iCarProofreadService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "网约车勘误表-批量删除")
    @ApiOperation(value = "网约车勘误表-批量删除", notes = "网约车勘误表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.iCarProofreadService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "网约车勘误表-通过id查询")
    @ApiOperation(value = "网约车勘误表-通过id查询", notes = "网约车勘误表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        CarProofread carProofread = iCarProofreadService.getById(id);
        if (carProofread == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(carProofread);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param carProofread
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CarProofread carProofread) {
        return super.exportXls(request, carProofread, CarProofread.class, "网约车勘误表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CarProofread.class);
    }
}
